package com.flycms.module.solution.service;

import java.util.List;
import java.util.Objects;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.solution.dao.SolutionPartnerDao;
import com.flycms.module.solution.model.SolutionPartner;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年3月10日
 * @Date 2020年3月10日
 */

@Service
public class SolutionPartnerService {

    @Autowired
    protected SolutionPartnerDao solutionPartnerDao;
    
    @Transactional
    public int addSolutionPartner(SolutionPartner solutionPartner) throws Exception {
        return solutionPartnerDao.addSolutionPartner(solutionPartner);
    }
    
    @Transactional
    public int updateSolutionPartner(SolutionPartner solutionPartner) throws Exception {
        return solutionPartnerDao.updateSolutionPartner(solutionPartner);
    }
    
    @Transactional
    public int addSolutionPartners(List<SolutionPartner> solutionPartners, Integer solutionId) throws Exception {
        return solutionPartnerDao.addSolutionPartners(solutionPartners, solutionId);
    }
    
    @Transactional
    public int updateSolutionPartners(List<SolutionPartner> solutionPartners, Integer solutionId, Boolean single) throws Exception {
        if (Objects.equals(single, false)) {
            solutionPartnerDao.removeSolutionPartners(solutionId, null);
            return solutionPartnerDao.addSolutionPartners(solutionPartners, solutionId);
        } else {
            int i = 0;
            for (SolutionPartner sp : solutionPartners) {
                solutionPartnerDao.updateSolutionPartner(sp);
                i++;
            }
            return i;
        }
    }
    
    @Transactional
    public int removeSolutionPartners(Integer solutionId, Integer partnerId) throws Exception {
        return solutionPartnerDao.removeSolutionPartners(solutionId, partnerId);
    }
    
    public List<SolutionPartner> getSolutionPartnerList(Integer solutionId, int pageIndex,
            int pageSize) throws Exception {
        return solutionPartnerDao.getSolutionPartnerList(solutionId, pageIndex, pageSize);
    }
    
    public int getSolutionPartnerCount(Integer solutionId) {
        return solutionPartnerDao.getSolutionPartnerCount(solutionId);
    }
    
}
